﻿
using System;
using System.Collections.Generic;

using YouHP.Utility;
using YouHP.BLL;
using YouHP.Model;
using System.Text;

namespace YouHP.Pages.Member
{
    public class LabcheckRecordes : YouHP.Pages.Member.PatientBasePage
    {
        
        protected override void ShowPage()
        {
            base.ShowPage();
            if (ErrorCount > 0) return;

            HandleCommon();
            if (IsPost) HandlePost();
            else HandleGet();
        }


        protected List<DiseaseLabcheckInfo> LabcheckList { get; set; }
        protected LabcheckRecordesInfo LabcheckRecordesInfo;
        protected string RecordDate;
        private Dictionary<int, string> _LabcheckRecordData;

        protected void HandleCommon()
        {
        }

        protected void HandlePost()
        {
            LabcheckList = DiseaseBLL.Instance.GetDiseaseLabcheckList(PRInfo.DiseaseId);
            int labRecordId = RequestTools.GetFormInt("labrecordid", -1);
            LabcheckRecordesInfo info = PatientBLL.Instance.GetLabcheckRecordes(labRecordId,PRInfo.Id);
            this.SetLabcheckRecordData(info.LabcheckInfo);

            bool isCreated = true;
            if (info.Id <= 0) isCreated = false;
            info.PRId = PRInfo.Id;
            
            foreach (DiseaseLabcheckInfo labinfo in LabcheckList)
            {
                AddLabcheckRecord(labinfo.LabcheckId, RequestTools.GetFormString("lab" + labinfo.LabcheckId));
            }

            info.LabcheckInfo = this.GetLabcheckRecordesDataString(); ;
            info.Remark = RequestTools.GetFormString("remark");
            info.RecordDate = Convert.ToDateTime(RequestTools.GetFormString("recorddate"));
            info.UpdateDate = DateTime.Now;


            if (isCreated)
            {
                PatientBLL.Instance.UpdateLabcheckRecordes(info);
                this.AddMsgLine("更新实验室指标记录成功");
                ResponseHelper.Redirect("ListLabcheckRecordes.aspx");
            }
            else
            {
                PatientBLL.Instance.AddLabcheckRecordes(info);
                this.AddMsgLine("添加实验室指标记录成功");
                ResponseHelper.Redirect("ListLabcheckRecordes.aspx");
            }
        }

        protected void HandleGet()
        {
            LabcheckList = DiseaseBLL.Instance.GetDiseaseLabcheckList(PRInfo.DiseaseId);
            int labid = RequestTools.GetQueryInt("id",-1);
            LabcheckRecordesInfo = PatientBLL.Instance.GetLabcheckRecordes(labid, PRInfo.Id);
            if (LabcheckRecordesInfo.Id > 0) RecordDate = LabcheckRecordesInfo.RecordDate.ToString("yyyy-MM-dd");
            else RecordDate = DateTime.Now.ToString("yyyy-MM-dd");
            this.SetLabcheckRecordData(LabcheckRecordesInfo.LabcheckInfo);
        }

        private void SetLabcheckRecordData(string datastr)
        {
            if (_LabcheckRecordData == null) _LabcheckRecordData = new Dictionary<int, string>();
            _LabcheckRecordData.Clear();
            string[] spliter = new string[] { "@" };
            string[] spliter2 = new string[] { "=" };
            string[] datapair = datastr.Split(spliter, StringSplitOptions.RemoveEmptyEntries);
            foreach (string str in datapair)
            {
                string[] strArray = str.Split(spliter2, StringSplitOptions.RemoveEmptyEntries);
                int key = Convert.ToInt32(strArray[0]);
                string val = strArray[1];
                _LabcheckRecordData.Add(key, val);
            }
        }
        protected string GetLabcheckRecordData(int key)
        {
            if (_LabcheckRecordData == null) return "";
            if (_LabcheckRecordData.ContainsKey(key) == false) return "";
            string data = _LabcheckRecordData[key];
            return data;
        }
        protected void AddLabcheckRecord(int key, string val)
        {
            if (_LabcheckRecordData.ContainsKey(key) == false)
            {
                _LabcheckRecordData.Add(key, val);
            }
            else
            {
                _LabcheckRecordData[key] = val;
            }
        }
        protected string GetLabcheckRecordesDataString()
        {
            StringBuilder sb = new StringBuilder();
            foreach (KeyValuePair<int, string> item in _LabcheckRecordData)
            {
                sb.AppendFormat("@{0}={1}", item.Key, item.Value);
            }
            return sb.ToString();
        }
    }
}